 #-*- coding: utf-8 -*-

import time
import random
import httplib 
import logging
 
'''
  <CONFIGURATION SECTION>
'''
 
#WMS_HTTP_HOST      		= '192.168.2.62:8079'
WMS_HTTP_HOST      		= '192.168.2.66:8079'
#WMS_HTTP_HOST      		= '192.168.2.62:8080'
WMS_PATH			= "/geoserver/wms"
WMS_OUTPUT_PATH		= "wmsResponse"
WMS_OUTPUT_SWITCH	       = False
WMS_STRING_LAM72		= "?LAYERS=Agiv%3AZomervlucht2009RGB&TRANSPARENT=true&FORMAT=image%2Ftiff&TILED=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A31370&BBOX=XMIN,YMIN,XMAX,YMAX&WIDTH=256&HEIGHT=256"
WMS_STRING_GOOGLE		= "?LAYERS=Agiv%3AZomervlucht2009RGB&TRANSPARENT=true&FORMAT=image%2Ftiff&TILED=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A900913&BBOX=XMIN,YMIN,XMAX,YMAX&WIDTH=256&HEIGHT=256"
WMS_STRING_WGS84		= "?LAYERS=Agiv%3AZomervlucht2009RGB&TRANSPARENT=true&FORMAT=image%2Ftiff&TILED=false&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&SRS=EPSG%3A4326&BBOX=XMIN,YMIN,XMAX,YMAX&WIDTH=256&HEIGHT=256"



'''
  </CONFIGURATION SECTION>
'''


logger = logging.getLogger("ogc.wms")
  
 
 
 
def randomizeInFlandersInWGS84( inString ):
  xMin=3.7438 + ( 1.8*random.random() )
  yMin=50.9846 + ( 0.1* random.random() )
  xMax=xMin+(0.0005*random.random() )
  yMax=yMin+(0.0005*random.random() )
  result = inString.replace( 'XMIN',str(xMin) ).replace( 'XMAX',str(xMax) ).replace( 'YMIN',str(yMin) ).replace( 'YMAX',str(yMax) )
  return result
  
 
  
def randomizeInFlandersInGoogle900913( inString ):
  xMin=416761+1+( 120000*random.random() )
  yMin=6618582+1+( 7000 * random.random() )
  xMax=xMin+1+(100*random.random() )
  yMax=yMin+1+(100*random.random() )
  result = inString.replace( 'XMIN',str(xMin) ).replace( 'XMAX',str(xMax) ).replace( 'YMIN',str(yMin) ).replace( 'YMAX',str(yMax) )
  return result
    
  
  
def randomizeInFlandersInLambert72( inString ):
  xMin=106119+1+( 120000*random.random() )
  yMin=186232+1+( 7000 * random.random() )
  xMax=xMin+1+(100*random.random() )
  yMax=yMin+1+(100*random.random() )
  result = inString.replace( 'XMIN',str(xMin) ).replace( 'XMAX',str(xMax) ).replace( 'YMIN',str(yMin) ).replace( 'YMAX',str(yMax) )
  return result
  
  
def sendHttpWMSGetCapabilitiesRequest():
  
  theWMSCall = "%s%s" % ( WMS_PATH, "?request=GetCapabilities" )
  theWMSCall = randomizeInFlandersInLambert72( theWMSCall)
  
  logger.debug( "About to connect to [%s]" % WMS_HTTP_HOST )
  wmsConnection = httplib.HTTPConnection(WMS_HTTP_HOST)
  logger.debug ( "Gonna make GET [%s]" % theWMSCall )
  wmsConnection.request( "GET", theWMSCall)    
  
  response = wmsConnection.getresponse()  
  if response.status != 200:
    debug.error(  "Error during WMS %s" % response.reason )
  
  logger.debug( "Writing out to [%s]" % WMS_OUTPUT_PATH )
  if WMS_OUTPUT_SWITCH:
    open( WMS_OUTPUT_PATH, "w" ).write(response.read() )
  
  
  
def sendHttpWMSRequest( inWMSString, inRandomizer ):
  
  theWMSCall = "%s%s" % ( WMS_PATH,  inWMSString )
  theWMSCall = inRandomizer( theWMSCall)
  
  logger.debug( "About to connect to [%s]" % WMS_HTTP_HOST )
  wmsConnection = httplib.HTTPConnection(WMS_HTTP_HOST)
  logger.debug ( "Gonna make GET [%s]" % theWMSCall )
  wmsConnection.request( "GET", theWMSCall)
  #print theWMSCall
  response = wmsConnection.getresponse()
  if response.status != 200:
    debug.error(  "Error during WMS %s" % response.reason )
    
  if WMS_OUTPUT_SWITCH:  
    open( WMS_OUTPUT_PATH, "wb" ).write(response.read() )
  
  
  
  
  
  
def printProgramHeading():

  print "############################################"
  print "## title:     WMS Benchmark Tool        ####"
  print "## author:    Stephen Kempenaers        ####"
  print "############################################"

  
  
  
if __name__ == "__main__":

  logging.basicConfig(level=logging.INFO)

  printProgramHeading()

  sendHttpWMSGetCapabilitiesRequest()  
  
  count = 0
  starttime = time.time()
  while 1:
    sendHttpWMSRequest( WMS_STRING_LAM72, randomizeInFlandersInLambert72 )    
    sendHttpWMSRequest( WMS_STRING_GOOGLE, randomizeInFlandersInGoogle900913 ) 
    sendHttpWMSRequest( WMS_STRING_WGS84, randomizeInFlandersInWGS84 ) 
    count = count + 3
    if ( count % 50 ) == 0:
      theElapsedTime = time.time() - starttime
      logger.info( "Number of requests [%d] in [%d] seconds" % ( count, theElapsedTime ) )
      
      
      
      
    
  


  
